package controller;

import java.io.IOException;

import DAO.AdminAccountDAO;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import constants.StringConstants;

/**
 * Servlet implementation class AccountServlet
 */
public class AccountServlet extends HttpServlet {

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AccountServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if (action.equals(StringConstants.LOGOUT)) {
			HttpSession session = request.getSession();
			session.removeAttribute("ADMIN_USERNAME");
			response.sendRedirect("admin/admin-login.html");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String button = request.getParameter("btnAction");
		if (button.equals(StringConstants.LOGIN)) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");

			AdminAccountDAO dao = new AdminAccountDAO();
			if (dao.checkAuthenticate(username, password)) {
				HttpSession session = request.getSession();
				session.setAttribute("ADMIN_USERNAME", username);
				response.sendRedirect("admin/admin-home.jsp");
			} else {
				response.sendRedirect("admin/admin-login.html");
			}
		}
	}
}
